using Domain;
using Domain.Entities;
using Domain.Permissions;
using Domain.Services;
using Domain.Misc;
using System;
using ServerControls.ImageLink;
using UserInterface.Misc;
using UserInterface.UserControls;
using System.Configuration;

namespace UserInterface
{
    public partial class Board : System.Web.UI.MasterPage
    {
        private NavigationData navigationData = new NavigationData();
        private TitleGenerator titleGenerator = new TitleGenerator();
        private PermissionManager permissionManager;
        private string title;
        private string currentTemplate;

        protected void Page_Load(object sender, EventArgs e)
        {
            titleGenerator.BoardName = BoardSettingsManager.Instance.Settings.BoardName;
            title = titleGenerator.Generate();

            Session session = SessionManager.CurrentSession;
            currentTemplate = session.CurrentTemplate;
            StyleSheet.Attributes.Add("href", Server.UrlPathEncode("./Templates/" + currentTemplate + "/Style.css"));
            if (!session.IsGuest)
            {
                User user = session.User;

                BoardQuickLoginPlaceHolder.Visible = false;
                BoardUserMenuPlaceHolder.Visible = true;

                permissionManager = new PermissionManager(UserService.GetById(user.Id));
                BoardAdministratorMenuPlaceHolder.Visible = permissionManager.IsAdministrator();
            }
            else
            {
                BoardQuickLoginPlaceHolder.Visible = true;
                BoardUserMenuPlaceHolder.Visible = false;
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            BindNavigationData();
            BoardMessages.BindData();
        }

        public void AddPathItem(string name, string navigationUrl)
        {
            navigationData.AddPathItem(name, navigationUrl);
        }

        public void AddNavigationItem(string imageUrl, string navigationUrl, string description)
        {
            navigationData.AddNavigationItem(imageUrl, navigationUrl, description);
        }

        public void AddNavigationItem(string imageUrl, string navigationUrl, string description, string onClientClick)
        {
            navigationData.AddNavigationItem(imageUrl, navigationUrl, description, onClientClick);
        }

        public string HeaderText
        {
            get { return HeaderTextLiteral.Text; }
            set
            {
                HeaderTextLiteral.Text = value;
                HeaderPlaceHolder.Visible = true;
            }
        }

        private void BindNavigationData()
        {
            MapPathTop.SetData(navigationData);
            MapPathBottom.SetData(navigationData);

            NavigationButtonsTop.SetData(navigationData);
            NavigationButtonsBottom.SetData(navigationData);
        }

        public Messages Messages
        {
            get { return BoardMessages; }
        }

        public TitleGenerator TitleGenerator
        {
            get { return titleGenerator; }
        }

        protected string Title
        {
            get { return title; }
            set { title = value;  }
        }

        protected string CurrentTemplate
        {
            get { return currentTemplate; }
            set { currentTemplate = value; }
        }
    }
}
